package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private StrategyTagMapper strategyTagMapper;

    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Autowired
    private IDestinationService destinationService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {

        //dest.id 通过catalog.id: 1去查询得到dest.id
        StrategyCatalog strategyCatalog = strategyCatalogMapper.selectByPrimaryKey(strategyDetail.getCatalog().getId());
        Destination dest = new Destination();
        dest.setId(strategyCatalog.getDestId());
        strategyDetail.setDest(dest);

        //author.id  统一写1吧现在
        strategyDetail.setAuthor_id(1L);

        //summary  截取内容的前200个字
        StrategyContent strategyContent = strategyDetail.getStrategyContent();
        String content = strategyContent.getContent();
        if (content.length() > 200) {
            strategyDetail.setSummary(content.substring(0, 200));
        } else {
            strategyDetail.setSummary(content);
        }

        //isboard  通过目的地吐司 查询是不是中国  是的话设置为false
        List<Destination> destinations = destinationService.creatToasts(dest.getId());
        if (destinations.get(0) != null) {
            if (destinations.get(0).getId() == 1) {
                strategyDetail.setIsabroad(false);
            }else {
                strategyDetail.setIsabroad(true);
            }
        }

        //保存修改
        if (strategyDetail.getId() != null) {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
            strategyDetailMapper.deleteRelation(strategyDetail.getId());
        } else {
            //creatTime
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }

        //保存tag  注意去重
        String[] split = tags.split(",");
        for (String tag : split) {
            StrategyTag strategyTag = new StrategyTag();
            strategyTag.setName(tag);
            strategyTag.setState(StrategyTag.STATE_NORMAL);
            StrategyTag strategyTag1 = strategyTagMapper.queryByTagName(tag);
            if (strategyTag1 == null) {
                strategyTagMapper.insert(strategyTag);
            }
            strategyDetailMapper.insertRelation(strategyDetail.getId(),strategyTag.getId());
        }
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void changeState(Long id, int state) {

    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> getDetailTop3(Long id) {
        return strategyDetailMapper.selectDetailTop3(id);
    }

    @Override
    public void updateReplynum(Long detailId, int i) {
        strategyDetailMapper.updateReplynum(detailId, i);
    }

    @Override
    public void updateReidsStatis(StrategyDetail detail) {
        strategyDetailMapper.updateReidsStatis(detail);
    }

    @Override
    public List<StrategyDetail> listAll() {
        return strategyDetailMapper.listAll();
    }

}
